package com.guaoran.jvm;

import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author : gu cheng
 * @description : https://juejin.cn/post/7139741080597037063#comment
 * @date : 2023/3/27 9:53
 */
public class ThreadSleepDemo {
    public static AtomicInteger num = new AtomicInteger(0);
    public static void main(String[] args) throws InterruptedException {
        Thread.sleep(20000);
        Runnable runnable = () -> {
            for (int i = 0; i < 1000000000; i++) {
                num.getAndAdd(1);
//                if (i % 1000 == 0) {
                try {
                    Thread.sleep(0);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
//                }
            }
            System.out.println(System.currentTimeMillis() + "=" + Thread.currentThread().getName() + "执行结束!");
        };
        Thread t1 = new Thread(runnable);
        Thread t2 = new Thread(runnable);
        t1.start();
        t2.start();

        Thread.sleep(1000);
        System.out.println(System.currentTimeMillis() + "=num = " + num);
    }
}
